using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using FytSoa.Application.Sys.Dto;
using FytSoa.Common;
using FytSoa.Common.Extensions;
using FytSoa.Model.Sys;
using FytSoa.Repository.Interfaces;
using FytSoa.SugarCore;

namespace FytSoa.Application.Sys
{
    public class SysRoleService : MyContext, ISysRoleService
    {
        private readonly IBaseRepository<SysRole> _thisRepository;
        public SysRoleService(IBaseRepository<SysRole> thisRepository)
        {
            _thisRepository = thisRepository;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<ApiResult<Page<SysRole>>> PageList(PageParam param)
        {
            var result = new ApiResult<Page<SysRole>>();
            try
            {
                Expression<Func<SysRole, bool>> where = e => true;
                if (!string.IsNullOrEmpty(param.key))
                {
                    where = where.And(e => e.Name.Contains(param.key));
                }
                if (param.status != 0)
                {
                    where = where.And(e => e.Status == (param.status == 1 ? true : false));
                }
                result.Data = await _thisRepository.GetPagesAsync(where, m=>m.Sort,1,param.page,param.limit);
                var newList = new List<SysRole>();
                foreach (var item in result.Data.Items.Where(m => m.ParentId == "0").ToList())
                {
                    //查询角色
                    var tempRole = result.Data.Items.Where(m => m.ParentId == item.Id).ToList();
                    newList.Add(item);
                    foreach (var row in tempRole)
                    {
                        row.Name =row.Name;
                        newList.Add(row);
                    }
                }
                result.Data.Items = newList;
            }
            catch (Exception ex)
            {
                result.StatusCode = (int)HttpStatusCode.InternalServerError;
                result.Message = ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 查询角色组
        /// </summary>
        /// <returns></returns>
        public async Task<ApiResult<List<SysRoleGroupDto>>> GroupSelect()
        {
            var result = new ApiResult<List<SysRoleGroupDto>>();
            try
            {
                result.Data = await Db.Queryable<SysRole>().Where(m=>m.ParentId=="0" && !m.IsDel && m.Status).Select(m=>new SysRoleGroupDto() { 
                    label=m.Name,
                    value=m.Id
                }).ToListAsync();
            }
            catch (Exception ex)
            {
                result.StatusCode = (int)HttpStatusCode.InternalServerError;
                result.Message = ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<ApiResult<string>> Add(SysRole model)
        {
            var result = new ApiResult<string>();
            try
            {
                model.Id = Unique.Id();
                model.CreateUser = "admin";
                await _thisRepository.AddAsync(model);
            }
            catch (Exception ex)
            {
                result.StatusCode = (int)HttpStatusCode.InternalServerError;
                result.Message = ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<ApiResult<string>> Modify(SysRole model)
        {
            var result = new ApiResult<string>();
            try
            {
                await _thisRepository.UpdateAsync(model,m=>new {m.CreateUser,m.CreateTime,m.IsDel });
            }
            catch (Exception ex)
            {
                result.StatusCode = (int)HttpStatusCode.InternalServerError;
                result.Message = ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 查询实体
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<ApiResult<SysRole>> GetModel(string id)
        {
            var result = new ApiResult<SysRole>();
            try
            {
                await _thisRepository.GetModelAsync(m=>m.Id==id);
            }
            catch (Exception ex)
            {
                result.StatusCode = (int)HttpStatusCode.InternalServerError;
                result.Message = ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<ApiResult<string>> Delete(List<string> ids)
        {
            var result = new ApiResult<string>();
            try
            {
                await _thisRepository.DeleteAsync(m=>ids.Contains(m.Id));
            }
            catch (Exception ex)
            {
                result.StatusCode = (int)HttpStatusCode.InternalServerError;
                result.Message = ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 查询角色组,包含所有
        /// </summary>
        /// <returns></returns>
        public async Task<ApiResult<List<SysRoleGroupList>>> GroupSelectAll()
        {
            var result = new ApiResult<List<SysRoleGroupList>>();
            try
            {
                var list = await _thisRepository.GetListAsync(m=>!m.IsDel, m => m.Sort,1);
                var newList = new List<SysRoleGroupList>();
                foreach (var item in list.Where(m => m.ParentId == "0").ToList())
                {
                    var model = new SysRoleGroupList()
                    {
                        label=item.Name
                    };
                    //查询角色
                    var tempRole = list.Where(m => m.ParentId == item.Id).ToList();
                    foreach (var row in tempRole)
                    {
                        model.options.Add(new SysRoleGroupDto() {
                            label=row.Name,
                            value=row.Id
                        });
                    }
                    newList.Add(model);
                }
                result.Data = newList;
            }
            catch (Exception ex)
            {
                result.StatusCode = (int)HttpStatusCode.InternalServerError;
                result.Message = ex.Message;
            }
            return result;
        }
    }
}
